package com.haiheng.module_main.adapter

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.viewbinding.ViewBinding
import com.haiheng.common.bean.BookInfoBean
import com.haiheng.common.constant.RourePage
import com.haiheng.library_base.adapter.BaseAdapter
import com.haiheng.library_base.utils.DateUtils
import com.haiheng.module_main.R
import com.haiheng.module_main.databinding.BorrowBookItemBinding

/**
 *
 * @Des:借书列表
 * @Author: hwz
 * @Date:2022/5/6 0006
 * @Version: 1.0
 */
class BorrowBookListAdapter : BaseAdapter<BookInfoBean>() {

    public var pageType : RourePage? = null

    init {
        addOnClickListener(R.id.slSelect)
        addOnClickListener(R.id.slDetal)
    }
    override fun onCreateViewBinding(viewType: Int): (LayoutInflater, ViewGroup, Boolean) -> ViewBinding {
        return BorrowBookItemBinding::inflate
    }

    override fun onItemView(holder: ViewBindHolder, position: Int, item: BookInfoBean) {
        val binding = holder.binding as BorrowBookItemBinding
        binding.tvSerialNo.text = "${position+1}"
        binding.tvBookSn.text = item.bookSn
        binding.tvBookName.text = item.bookTitle
        binding.tvDate.setText("${DateUtils.formatTime(item.needReturnTime,DateUtils.DATE_FORMAT_4)}")
        if ("0".equals(item.errorCode)){
            binding.ivStatus.visibility = View.VISIBLE
            binding.tvStatus.setText(item.remarksMsg)
            setViewTectColor(binding.tvStatus,true)
        }else{
            binding.ivStatus.visibility = View.GONE
            binding.tvStatus.setText(item.remarksMsg)
            setViewTectColor(binding.tvStatus,false)
        }
        when(pageType){
            RourePage.BORROW_BOOK_PAGE ->{

            }
            RourePage.RETRUN_BOOK_PAGE ->{

            }
            RourePage.RENEWN_BOOK_PAGE ->{
                binding.slSelect.visibility = View.VISIBLE
                binding.tvBorrowStatus.visibility = View.VISIBLE
                binding.tvBookSn.visibility= View.GONE
                binding.llStatus.visibility = View.GONE
            }
        }
        binding.tvBorrowStatus.text = "${item.borrowStatus}"
        if ("超期".equals(item.borrowStatus) || !"0".equals(item.errorCode)){
            binding.tvBorrowStatus.setTextColor(ContextCompat
                .getColor( binding.tvBorrowStatus.context, com.haiheng.library_base.R.color.color_FF575))
        }else{
            binding.tvBorrowStatus.setTextColor(ContextCompat
                .getColor( binding.tvBorrowStatus.context, com.haiheng.library_base.R.color.text_333))
        }
        binding.slSelect.isSelected = item.isSelect
        binding.slSelect.isClickable = false
    }

    fun setViewTectColor(tv:TextView,isBorrowSucees:Boolean){
        if (isBorrowSucees){
            tv.setTextColor(ContextCompat.getColor(tv.context, com.haiheng.library_base.R.color.color_00B259))
        }else{
            tv.setTextColor(ContextCompat.getColor(tv.context, com.haiheng.library_base.R.color.color_FF575))
        }
    }
}